import Axios from "axios";
import { apiJava } from "@/api/root";
import { downLoadFile } from "@/utils/downLoadFile";
export default {
  data() {
    return {
      dataListSelections: [],
      exportLoading: false,
    };
  },
  methods: {
    selectChange(val) {
      this.dataListSelections = val;
    },
    selectAllChange(val) {
      this.dataListSelections = val;
    },
    handleExportContentData() {
      if (this.dataListSelections.length) {
        const contentIdList = this.dataListSelections.map((item, index) => {
          return item.contentId;
        });
        const params = {
          contentIdList,
          contentType: this.form.contentType,
        };
        this.exportLoading = true;
        Axios({
          method: "post",
          url: apiJava + "/api-occupation/content/exportArticleList",
          data: params,
          responseType: "arraybuffer",
          headers: {},
          timeout: "60000",
        })
          .then((res) => {
            if (res) {
              let fileName = "导出数据.xlsx";
              switch (this.contentType) {
                case 10:
                  fileName = "职讯" + fileName;
                  break;
                case 20:
                  fileName = "短视频" + fileName;
                  break;
                case 30:
                  fileName = "职业报告" + fileName;
                  break;
                case 40:
                  fileName = "直播" + fileName;
                  break;
                case 50:
                  fileName = "顾问" + fileName;
                  break;
                default:
                  break;
              }
              downLoadFile(res, fileName);
            } else {
              this.$message.error("导出错误");
            }
          })
          .catch(() => {
            this.$message.error("导出错误");
          })
          .finally(() => {
            this.exportLoading = false;
          });
      } else {
        this.$message.warning("请选择要导出的数据！");
      }
    },
    handleExportContentDataWithParams() {
      if (!this.isSearch) {
        this.$message.error("先进行搜索再导出！");
        return;
      }
      const params = {
        contentType: this.form.contentType,
        isSearch: this.isSearch,
        ...this.form,
      };
      this.exportLoading = true;
      Axios({
        method: "post",
        url: apiJava + "/api-occupation/content/exportArticleList",
        data: params,
        responseType: "arraybuffer",
        headers: {},
        timeout: "60000",
      })
        .then((res) => {
          if (res) {
            let fileName = "导出数据.xlsx";
            switch (this.contentType) {
              case 10:
                fileName = "职讯" + fileName;
                break;
              case 20:
                fileName = "短视频" + fileName;
                break;
              case 30:
                fileName = "职业报告" + fileName;
                break;
              case 40:
                fileName = "直播" + fileName;
                break;
              case 50:
                fileName = "顾问" + fileName;
                break;
              default:
                break;
            }
            downLoadFile(res, fileName);
          } else {
            this.$message.error("导出错误");
          }
        })
        .catch(() => {
          this.$message.error("导出错误");
        })
        .finally(() => {
          this.exportLoading = false;
        });
    },
    handleExportLiveData() {
      if (this.dataListSelections.length) {
        const contentIdList = this.dataListSelections.map((item, index) => {
          return item.liveId;
        });
        const params = {
          contentIdList,
          contentType: this.form.contentType,
        };
        this.exportLoading = true;
        Axios({
          method: "post",
          url: apiJava + "/api-occupation/content/exportLiveList",
          data: params,
          responseType: "arraybuffer",
          headers: {},
          timeout: "60000",
        })
          .then((res) => {
            if (res) {
              let fileName = "导出数据.xlsx";
              switch (this.contentType) {
                case 10:
                  fileName = "职讯" + fileName;
                  break;
                case 20:
                  fileName = "短视频" + fileName;
                  break;
                case 30:
                  fileName = "职业报告" + fileName;
                  break;
                case 40:
                  fileName = "直播" + fileName;
                  break;
                case 50:
                  fileName = "顾问" + fileName;
                  break;
                default:
                  break;
              }
              downLoadFile(res, fileName);
            } else {
              this.$message.error("导出错误");
            }
          })
          .catch(() => {
            this.$message.error("导出错误");
          })
          .finally(() => {
            this.exportLoading = false;
          });
      } else {
        this.$message.warning("请选择要导出的数据！");
      }
    },
    exportDataPost(url, data, fileName) {
      this.exportLoading = true;
      Axios({
        method: "post",
        url: apiJava+"/api-occupation/" + url,
        data,
        responseType: "arraybuffer",
        headers: {},
        timeout: "60000",
      })
        .then((res) => {
          if (res) {
            downLoadFile(res, fileName);
          } else {
            this.$message.error("导出错误");
          }
        })
        .catch(() => {
          this.$message.error("导出错误");
        })
        .finally(() => {
          this.exportLoading = false;
        });
    },
    exportData(url, params, fileName) {
      this.exportLoading = true;
      Axios({
        method: "get",
        url: apiJava+"/api-occupation/" + url,
        params,
        responseType: "arraybuffer",
        headers: {},
        timeout: "60000",
      })
        .then((res) => {
          if (res) {
            downLoadFile(res, fileName);
          } else {
            this.$message.error("导出错误");
          }
        })
        .catch(() => {
          this.$message.error("导出错误");
        })
        .finally(() => {
          this.exportLoading = false;
        });
    },
  },
};
